package com.lsh.oms.core.service.bills.impl;

import com.alibaba.fastjson.JSON;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.dao.other.OrderRoDetailMapper;
import com.lsh.oms.core.dao.other.OrderRoHeadMapper;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.model.bills.OrderReturnVo;
import com.lsh.oms.core.model.order.OrderRoDetail;
import com.lsh.oms.core.model.order.OrderRoHead;
import com.lsh.oms.core.mq.MessageProducer;
import com.lsh.oms.core.service.other.RoOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author peter
 * @date 18/11/5
 */

@Service("orderReturnTmsService")
@Slf4j
public class OrderReturnKafkaService extends AbstractKafkaMessageService<OrderReturnVo> {

    @Autowired
    private OrderRoHeadMapper orderRoHeadMapper;

    @Autowired
    private OrderRoDetailMapper orderRoDetailMapper;

    @Value("${kafka.topic.of.return.order:oms_return_order}")
    private String topic;

    @Resource(name = "kafka.message.producer")
    private MessageProducer producer;

    @Resource
    private RoOrderService returnOrderService;

    @Override
    public void doBiz(OrderReturnVo obj) {
        String prefix = "返仓单处理";
        try {
            OrderRoHead orderReturnHead = obj.getOrderReturnRead();
            List<OrderRoDetail> details = obj.getOrderReturnDetail();
            log.info("{} return order message: {}", prefix, JSON.toJSONString(orderReturnHead));
            OrderRoHead cell = orderRoHeadMapper.selectByReturnOrderId(orderReturnHead.getReturnOrderId());
            if (cell == null) {
                log.info("{} insert : {}", prefix, orderReturnHead.getReturnOrderId());
                orderReturnHead.setTmsId(orderReturnHead.getId());
                orderReturnHead.setId(null);
                orderRoHeadMapper.insertSelective(orderReturnHead);
                for (OrderRoDetail detail : details) {
                    detail.setTmsId(detail.getId());
                    detail.setId(null);
                    orderRoDetailMapper.insertSelective(detail);
                }
            } else {
                log.info("{} update : {}", prefix, orderReturnHead.getReturnOrderId());
                orderReturnHead.setTmsId(orderReturnHead.getId());
                orderReturnHead.setId(null);
                orderRoHeadMapper.updateByTmsId(orderReturnHead);
                for (OrderRoDetail detail : details) {
                    detail.setTmsId(detail.getId());
                    detail.setId(null);
                    orderRoDetailMapper.updateByTmsId(detail);
                }
            }
            log.info("{} return order insert or update end: {}", prefix, orderReturnHead.getReturnOrderId());
            pushToMQ(orderReturnHead.getReturnOrderId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(ReturnCode.UPDATE_FAIL, "order return service db exception", e);
        }
    }

    private void pushToMQ(Long id) {
        Map<String, Object> message = returnOrderService.findReturnOrderById(id);
        producer.send(topic, JSON.toJSONString(message));
    }

    @Override
    public OrderReturnVo parse(String json) {
        return JSON.parseObject(json, OrderReturnVo.class);
    }
}
